﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MxTol.Managers.Helpers;

namespace MxTol.Managers
{
    /// <summary>
    /// 学生账号管理器
    /// </summary>
    public class StudentManager
    {
        /// <summary>
        /// 获取所有学生(Json分页格式)
        /// </summary>
        public static string GetAllStudent(int currentPage, int pageSize)
        {
            string json = string.Empty;

            try
            {
                string sql = "select * from Student;";
                json = PaginationHelper.GetJsonBySql(currentPage, pageSize, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return json;
        }

        /// <summary>
        /// 获取所有学生(Json分页格式)
        /// </summary>
        public static string GetAllStudent(int currentPage, int pageSize, string sql)
        {
            string json = string.Empty;

            try
            {
                json = PaginationHelper.GetJsonBySql(currentPage, pageSize, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return json;
        }

        /// <summary>
        /// 判断指定账号是否存在
        /// </summary>
        public static bool IsAccountExsit(string account)
        {
            bool isExist = false;
            try
            {
                string sql = string.Format("select count(id) from Student where account = '{0}';", account);
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;
        }

        /// <summary>
        /// 判断密码和账号是否匹配
        /// </summary>
        public static bool IsPswordMatch(string account, string psword)
        {
            bool isMatch = false;
            try
            {
                string sql = string.Format("select count(id) from Student where account = '{0}' and  psword = '{1}';", account, psword.ToLower());
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isMatch = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isMatch;
        }

        /// <summary>
        /// 判断账号和安卓客户端机器码是否匹配
        /// </summary>
        public static bool IsAndroidCodeMatch(string account, string code)
        {
            bool isMatch = false;
            try
            {
                string sql = string.Format("select count(id) from Student where account = '{0}' and  android_code = '{1}';", account, code);
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isMatch = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isMatch;
        }

        /// <summary>
        /// 判断指定姓名的学生是否存在
        /// </summary>
        public static bool IsNameExist(string name)
        {
            bool isExist = false;

            try
            {
                string sql = string.Format("select count(id) from Student where name = '{0}';", name);
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;
        }

        /// <summary>
        /// 判断指定号码的学生是否存在
        /// </summary>
        public static bool IsPhoneExist(string phone)
        {
            bool isExist = false;
            try
            {
                string sql = string.Format("select count(id) from Student where phone = '{0}';", phone);
                int count = int.Parse(SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql).ToString());
                if (count != 0)
                {
                    isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;
        }

        /// <summary>
        /// 设置学生账号的密码
        /// </summary>
        public static bool SetPsword(string account, string psword)
        {
            bool isSuccess = false;
            try
            {
                string sql = string.Format("update Student set psword = '{0}' where account ='{1}';", psword, account);
                int count = SqlHelper.ExecuteNonQuery(ManagerConfig.ConnectionString, sql);
                if (count != 0)
                {
                    isSuccess = true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return isSuccess;
        }

        /// <summary>
        /// 设置学生账号上一次登录时间
        /// </summary>
        public static bool SetLastLoginTime(string account, DateTime time)
        {
            bool isSuccess = false;
            try
            {
                string sql = string.Format("update Student set last_login_time = '{0}' where account ='{1}';", time.ToString("yyyy-MM-dd HH:mm:ss"), account);
                int count = SqlHelper.ExecuteNonQuery(ManagerConfig.ConnectionString, sql);
                if (count != 0)
                {
                    isSuccess = true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return isSuccess;
        }

        /// <summary>
        /// 获取学生账号上一次登录时间
        /// </summary>
        public static Nullable<DateTime> GetLastLoginTime(string account)
        {
            Nullable<DateTime> time = null;
            try
            {
                string sql = string.Format("select last_login_time from Student where account ='{0}';", account);
                time = Convert.ToDateTime( (SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql)).ToString());
            }
            catch (Exception)
            {
                time= null;
            }
            return time;
        }

        /// <summary>
        ///  设置学生账号的在线状态
        /// </summary>
        public static bool SetOnline(string account, bool isOnline)
        {
            bool isSuccess = false;
            int online = isOnline ? 1 : 0;
            try
            {
                string sql = string.Format("update Student set is_online = {0} where account ='{1}';", online, account);
                int count = SqlHelper.ExecuteNonQuery(ManagerConfig.ConnectionString, sql);
                if (count != 0)
                {
                    isSuccess = true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return isSuccess;
        }

        /// <summary>
        /// 根据学生账号获取安卓客户端机器码
        /// </summary>
        public static string GetAndoridCode(string account)
        {
            string code = string.Empty;
            try
            {
                string sql = string.Format("select android_code from Student where account ='{0}';", account);
                code = (SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql)).ToString();
            }
            catch (Exception)
            {
                throw;
            }
            return code;
        }

        /// <summary>
        ///  设置学生安卓客户端机器码
        /// </summary>
        public static bool SetAndroidCode(string account, string Code)
        {
            bool isSuccess = false;
            try
            {
                string sql = string.Format("update Student set android_code = '{0}' where account ='{1}';", Code, account);
                int count = SqlHelper.ExecuteNonQuery(ManagerConfig.ConnectionString, sql);
                if (count != 0)
                {
                    isSuccess = true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return isSuccess;
        }

        /// <summary>
        /// 获取学生姓名
        /// </summary>
        public static string GetName(string account)
        {
            string name = string.Empty;
            try
            {
                string sql = string.Format("select name from Student where account ='{0}';", account);
                name = (SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql)).ToString();
            }
            catch (Exception)
            {
                throw;
            }
            return name;
        }

        /// <summary>
        /// 获取学生电话号码
        /// </summary>
        public static string GetPhone(string account)
        {
            string phone = string.Empty;
            try
            {
                string sql = string.Format("select phone from Student where account ='{0}';", account);
                phone = (SqlHelper.ExecuteScalar(ManagerConfig.ConnectionString, sql)).ToString();
            }
            catch (Exception)
            {
                throw;
            }
            return phone;
        }
    }
}
